package com.zyh.properties;

import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;

/**
 * @Author zyh
 * @Date 2024/8/6 16:38
 * @Description Properties 集合和文件相关操作
 */

public class PropertiesDemo2 {
    /*
    将集合中的数据存入到文件中
     */
    public static void main1(String[] args) throws IOException {
        Properties prop=new Properties();
        prop.setProperty("zyh","20");
        prop.setProperty("cxk","22");
        prop.setProperty("dz","34");
        prop.store(new FileWriter("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day10-code\\src\\com\\zyh\\resources\\mysql.properties"),"key=value");
    }


    /*
    从文件中读取数据到集合中
     */
    public static void main(String[] args) throws IOException {
        Properties prop = new Properties();
        prop.load(new FileReader("D:\\Code\\IDEA\\idea_projects\\JavaSE-plus\\day10-code\\src\\com\\zyh\\resources\\mysql.properties"));
        Set<String> keys = prop.stringPropertyNames();
        for (String key : keys) {
            System.out.println(key+"--"+prop.getProperty(key));
        }
    }
}
